package com.comm.exception;

import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.comm.enums.SentinelErrorEnum;
import com.comm.result.ResponseResult;
import feign.RetryableException;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/7/9
 **/
public class SentinelExceptionHandler {
    public static ResponseResult<String> sentinelExceptionHandler(Throwable e) {
        ResponseResult<String> result = null;
        if (e instanceof FlowException) {
            //服务限流
            result = ResponseResult.error(SentinelErrorEnum.SERVE_LIMIT.getCode(), SentinelErrorEnum.SERVE_LIMIT.getMsg(), null);
        } else if (e instanceof DegradeException) {
            //服务熔断
            result = ResponseResult.error(SentinelErrorEnum.SERVE_CIRCUIT.getCode(), SentinelErrorEnum.SERVE_CIRCUIT.getMsg(), null);
        } else if (e instanceof ParamFlowException) {
            //热点限流
            result = ResponseResult.error(SentinelErrorEnum.SERVE_HOT_LIMIT.getCode(), SentinelErrorEnum.SERVE_HOT_LIMIT.getMsg(), null);
        } else if (e instanceof SystemBlockException) {
            //系统自适应限流
            result = ResponseResult.error(SentinelErrorEnum.SERVE_SELF_ADAPTIVE_LIMIT.getCode(), SentinelErrorEnum.SERVE_SELF_ADAPTIVE_LIMIT.getMsg(), null);
        } else if (e instanceof AuthorityException) {
            //没有授权
            result = ResponseResult.error(SentinelErrorEnum.SERVE_NO_AUTHORITY.getCode(), SentinelErrorEnum.SERVE_NO_AUTHORITY.getMsg(), null);
        } else if (e instanceof RetryableException) {
            //服务熔断
            result = ResponseResult.error(SentinelErrorEnum.SERVE_CIRCUIT.getCode(), SentinelErrorEnum.SERVE_CIRCUIT.getMsg(), null);
        } else {
            result = ResponseResult.isSuccess();
        }
        return result;
    }
}
